package pers.cz.postgirl.adapter.http.servlet;

import pers.cz.postgirl.adapter.AbstractTransformer;
import pers.cz.postgirl.config.EvenType;
import pers.cz.postgirl.core.EnhanceModel;
import pers.cz.postgirl.core.common.CpublicFuncCallBack;

import java.util.Map;

/**
 * @program: postgirl-client
 * @description: http增强
 * @author: Cheng Zhi
 * @create: 2023-05-23 19:26
 **/
public class HttpPlugin extends AbstractTransformer {

    @Override
    public void loadEnhanceModel() {

        EnhanceModel.MethodPattern.DefaultCodePattern defaultCodePattern = EnhanceModel.MethodPattern.DefaultCodePattern.builder()
                .beforeCode(getBeforeSrc())
                .returnCode(getEndSrc())
                .build();

        EnhanceModel.MethodPattern mpBefore = EnhanceModel.MethodPattern.builder()
                .methodName("service")
                .paramTypes(new String[]{"javax.servlet.http.HttpServletRequest", "javax.servlet.http.HttpServletResponse"})
                .evenTypes(new EvenType[] {EvenType.BEFORE, EvenType.AFTER})
                .defaultCodePattern(defaultCodePattern)
                .build();

        enhanceModel = EnhanceModel.builder()
                .classPattern("javax.servlet.http.HttpServlet")
                .methodPatterns(new EnhanceModel.MethodPattern[]{mpBefore})
                .build();
    }


    private CpublicFuncCallBack getBeforeSrc() {
        return new CpublicFuncCallBack() {
            @Override
            public String execute() throws Exception{
                StringBuilder src = new StringBuilder();
                src.append("try {\n");
                src.append("            javax.servlet.http.HttpServletRequest my_req = $1;\n");
                src.append("            String caseId = my_req.getHeader(\"caseId\");\n");
                src.append("            String type = my_req.getHeader(\"type\");\n");
                src.append("            pers.cz.postgirl.adapter.http.servlet.HttpProcess.handle(caseId, type);\n");
                src.append("        } catch (Exception e) {\n");
                src.append("            e.printStackTrace();\n");
                src.append( "        }");
                return src.toString();
            }

            @Override
            public CpublicFuncCallBack setParamters(Map<String, Object> map)  throws Exception{
                return null;
            }
        };
    }

    private CpublicFuncCallBack getEndSrc() {

        return new CpublicFuncCallBack() {
            @Override
            public String execute() {
                StringBuilder src = new StringBuilder();
                src.append("try {\n");
                src.append("           if (caseId != null) {\n");
                src.append("               pers.cz.postgirl.util.ContextUtils.remoteAll();\n");
                src.append("            }");
                src.append("        } catch (Exception e) {\n");
                src.append("            e.printStackTrace();\n");
                src.append( "        }");
                return src.toString();
            }

            @Override
            public CpublicFuncCallBack setParamters(Map<String, Object> map) {
                return null;
            }
        };
    }
}
